﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BCL;
using UploadingControl.SilverlightService;
using System.Windows.Browser;

namespace UploadingControl
{
    public partial class NewDetailPopup : ChildWindow
    {
        PageMode Mode;
        public LogoDetail LogoDetail { get; set; }
        public string FileFormat { get; set; }
        public NewDetailPopup(PageMode pageMode, LogoDetail logoDetail, string fileFormat)
        {

            InitializeComponent();
            this.FileFormat = fileFormat;
            Mode = pageMode;
            LogoDetail = logoDetail;
            this.Title = logoDetail.MenuName;
            SetFormat(uploadWithPageNumber1);
            SetFormat(uploadWithPageNumber2);
            SetFormat(uploadWithPageNumber3);
            SetFormat(uploadWithPageNumber4);
            SetFormat(uploadWithPageNumber5);

            dataGrid1.ItemsSource = LogoDetail.DetailToDetail;
            txtTags.Text = logoDetail.Tags ?? string.Empty;
        }

        public void SetFormat(UploadWithPageNumber control)
        {
            control.FileFormat = FileFormat;
            control.Title = this.Title as string;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LogoDetail.Tags = txtTags.Text;
                LogoDetail.NoOfFilePerPage = Convert.ToInt32(textBox1.Text);
            }
            catch
            {
                LogoDetail.NoOfFilePerPage = 4;
            }
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (uploadWithPageNumber1.CanUploadFile) AddDetail(uploadWithPageNumber1);
            if (uploadWithPageNumber2.CanUploadFile) AddDetail(uploadWithPageNumber2);
            if (uploadWithPageNumber3.CanUploadFile) AddDetail(uploadWithPageNumber3);
            if (uploadWithPageNumber4.CanUploadFile) AddDetail(uploadWithPageNumber4);
            if (uploadWithPageNumber5.CanUploadFile) AddDetail(uploadWithPageNumber5);
            dataGrid1.ItemsSource = LogoDetail.DetailToDetail.OrderBy(f => f.OrderNo).ToList();
        }

        private void AddDetail(UploadWithPageNumber control)
        {
            LogoDetailDetail log = null;
            if (LogoDetail.DetailToDetail != null)
                log = LogoDetail.DetailToDetail.Where(f => f.OrderNo == control.OrderNo).FirstOrDefault();
            if (log == null)
            {

                LogoDetailDetail logoDetailDetail = new LogoDetailDetail();
                logoDetailDetail.FileName = control.FileName;
                logoDetailDetail.Description = control.Description;
                logoDetailDetail.ThumbnailUrl = control.Thumbnail;
                logoDetailDetail.LogoDetailId = 0;
                logoDetailDetail.OrderNo = Convert.ToInt32(control.OrderNo);
                if (LogoDetail.DetailToDetail == null)
                    LogoDetail.DetailToDetail = new List<LogoDetailDetail>();

                LogoDetail.DetailToDetail.Add(logoDetailDetail);


                control.Reset();
            }
            else
            {
                MessageBox.Show(string.Format("{0} order no already exist please insert other sequence", control.OrderNo), "Warning", MessageBoxButton.OK);
            }
        }

        private void hbtnView_Click(object sender, RoutedEventArgs e)
        {
            var v = dataGrid1.SelectedItem as LogoDetailDetail;
            HtmlPage.Window.Invoke("OpenPreView", new string[] { v.FileName });
        }

        private void hbtnEdit_Click(object sender, RoutedEventArgs e)
        {
            var v = dataGrid1.SelectedItem as LogoDetailDetail;
            LogoDetail.DetailToDetail.Remove(v);
        }


    }
}

